package org.groupg.practice.jetty;

import org.eclipse.jetty.proxy.ProxyHandler;
import org.eclipse.jetty.server.*;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.util.thread.QueuedThreadPool;

public class ProxyPractice {
    public static void main(String[] args) {

        // Create and configure a ThreadPool.
        QueuedThreadPool threadPool = new QueuedThreadPool();
        threadPool.setName("server");

// Create a Server instance.
        Server server = new Server(threadPool);

// Create a ServerConnector to accept connections from clients.
        Connector connector = new ServerConnector(server);

// Add the Connector to the Server
        server.addConnector(connector);

// Set a simple Handler to handle requests/responses.
        server.setHandler(new Handler.Abstract() {
            @Override
            public boolean handle(Request request, Response response, Callback callback) {
                // Succeed the callback to signal that the
                // request/response processing is complete.
                callback.succeeded();
                return true;
            }
        });

        Handler.Sequence sequence = new Handler.Sequence();
        sequence.addHandler(new ProxyHandler.Reverse("https://admin.com/proxy", "https://proxyServer.com/*"));
        sequence.addHandler(new ProxyHandler.Reverse("https://admin.com/proxy1", "https://proxyServer1.com/*"));
//        server.setHandler(new ProxyHandler.Reverse("https://admin.com/proxy", "https://proxyServer.com/*"));
        server.setHandler(sequence);
        try {
            server.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
